//
//  main.swift
//  GroupPattern
//
//  Created by mac on 2017/11/15.
//  Copyright © 2017年 RRC. All rights reserved.
//

import Foundation

print("Hello, World!")

////产生产生一个根节点
//let ceo:IRoot = Root(name: "王大麻子", position: "总经理", salary: 100000)
////产生三个部门经理
//let developerDep:IBranch = Branch(name: "刘大瘸子", position: "研发部经理", salary: 10000)
//let salesDep:IBranch = Branch(name: "赵三驼子", position: "财务经理", salary: 10000)
//let financeDep:IBranch = Branch(name: "马三拐子", position: "销售部经理", salary: 10000)
////再把三个小组长产生出来
//let fistDevGrop:IBranch = Branch(name: "杨三乜斜", position: "开发一组组长", salary: 5000)
//let secondDevGrop:IBranch = Branch(name: "吴大棒槌", position: "开发三组组长", salary: 6000)
////剩下的就是我们这些小兵了
//let a:ILeaf = Leaf(name: "a", position: "开发人员", salay: 2000)
//let b:ILeaf = Leaf(name: "b", position: "开发人员", salay: 2000)
//let c:ILeaf = Leaf(name: "c", position: "开发人员", salay: 2000)
//let h:ILeaf = Leaf(name: "h", position: "销售人员", salay: 2000)
//let i:ILeaf = Leaf(name: "i", position: "财务人员", salay: 2000)
//let k:ILeaf = Leaf(name: "k", position: "CEO秘书", salay: 2000)
//
////该生产的人都产生出来了，然后组装这棵树
//ceo.add(branch: developerDep)
//ceo.add(branch: salesDep)
//ceo.add(branch: financeDep)
////总经理下还有一个秘书
//ceo.add(leaf: k)
//
////定义研发部门下的结构
//developerDep.add(branch: fistDevGrop)
//developerDep.add(branch: secondDevGrop)
////看看开发两个不组有什么
//fistDevGrop.add(leaf: a)
//fistDevGrop.add(leaf: b)
//secondDevGrop.add(leaf: c)
////再看销售部下人员情况
//salesDep.add(leaf: h)
////最后一个财务部
//financeDep.add(leaf: i)
////打印写完的树状结构
//print(ceo.getInfo())
//
//
//
//func getAllSubordinateInfo(subordinateList:[Any]){
//    for index in subordinateList {
//
//        if index is Leaf{
//           print((index as! Leaf).getInfo())
//        }else{
//            print((index as! Branch).getInfo())
//            getAllSubordinateInfo(subordinateList: (index as! Branch).getSubordinateInfo())
//        }
//
////        if ((index as? Leaf) != nil) {
////            print((index as! Leaf).getInfo())
////        }else{
////            print((index as! Branch).getInfo())
////            getAllSubordinateInfo(subordinateList: (index as! Branch).getSubordinateInfo())
////        }
//    }
//}
//
//getAllSubordinateInfo(subordinateList: ceo.getSubordinateInfo())


public func compositeCorpTree() -> Branch {
    let root = Branch(name: "王在麻子", position: "总经理", salary: 100000)
    let developDep = Branch(name: "马二拐子", position: "销售部门经理", salary: 20000)
    let financeDep = Branch(name: "赵三驼子", position: "财务部经理", salary: 30000)
    let salesDep = Branch(name: "刘大瘸子", position: "研发部门经理", salary: 10000)
    
    let firstDevGroup = Branch(name: "杨三乜斜", position: "开发一组组长", salary: 5000)
    let secondDevGroup = Branch(name: "吴大棒槌", position: "开发二组组长", salary: 6000)
    
    let a = Leaf(name: "a", position: "开发人员", salay: 2000)
    let b = Leaf(name: "b", position: "开发人员", salay: 2000)
    let c = Leaf(name: "c", position: "开发人员", salay: 2000)
    let h = Leaf(name: "h", position: "销售人员", salay: 2000)
    let i = Leaf(name: "i", position: "财务人员", salay: 2000)
    let k = Leaf(name: "k", position: "CEO秘书", salay: 2000)
    
    //开始组装
    root.addSubordinate(corp: k)
    root.addSubordinate(corp: developDep)
    root.addSubordinate(corp: salesDep)
    root.addSubordinate(corp: financeDep)
    
    developDep.addSubordinate(corp: firstDevGroup)
    developDep.addSubordinate(corp: secondDevGroup)
    
    firstDevGroup.addSubordinate(corp: a)
    firstDevGroup.addSubordinate(corp: b)
    secondDevGroup.addSubordinate(corp: c)
    
    salesDep.addSubordinate(corp: h)
    
    financeDep.addSubordinate(corp: i)
   
    return root
}

//遍历整棵树，只要给我根节点，我就能遍历出所有的节点
func getTreeInfo(root:Branch) -> String {
//    let subordinateList:[ICorp] = root.getSubordinate()
    let subordinateList = root.getSubordinate()
    var info = ""
    for obj in subordinateList {
        if obj is Leaf{
            info = info + obj.getInfo() + "\n"
            obj.find()
        }else{
            info = info + obj.getInfo() + "\n" + getTreeInfo(root: obj as! Branch)
        }
    }
    return info
}

let ceo = compositeCorpTree()
print(ceo.getInfo())
print(getTreeInfo(root: ceo))
/**
 组合模式的定义：将对象组合成树形结构以表示“部分-整体”的层次结构，使得用户对单个对象和组合对象的使用具有一致性。
 */
